package com.example.mongo.service;

import com.example.mongo.Model.bean.User;
import com.example.mongo.dao.UserMapper;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
public class UserService  {

    @Autowired
    public UserMapper userMapper;

    public User getUserById(String id) {
        return userMapper.getUserById(id);
    }
    
    public User getUserByUserName(String userName) {
        return userMapper.getUserByUserName(userName);
    }

    public List<User> getUsersByIsVIP(int isVIP) {
        return userMapper.getUsersByIsVIP(isVIP);
    }

    @Transactional
    public boolean deleteUser(String id) {

         userMapper.deleteById(id);             //删除

         User user = userMapper.getUserById(id);// 删除再查找 ，为空删除成功！

         if (null==user){
             return true;
         }else {
             return false;
         }

    }
    @Transactional
    public User updatePassword(User user) {
        //userMapper.getUserByUserName(user.getUserName());
        return userMapper.save(user);
    }

    @Transactional
    public User updateUserCoin(User user) {
        //userMapper.getUserByUserName(user.getUserName());
        return userMapper.save(user);
    }
    //开通几个月的会员
    @Transactional
    public User getVIP(User user,int month) {
        user.setDeadLine(1000*60*60*24*30*month+ new Date().getTime());
        user.setIsVIP(1);
        user.setUserCoin(user.getUserCoin()-150*month);
        return userMapper.save(user);
    }
    @Transactional
    public User insertAdmin(User user) {
        //超大型分布式项目如何保证 id 唯一 ，可以借用ip，时间 + 随机数 + 进程id ；  登录时存储在Redis中更需要注意同时登录的人远比同时注册的人多。
        user.setId(new ObjectId(new Date()).toString());
        user.setCreateDate(new Date());
        user.setIsVIP(2);
        user.setUserCoin(0);
        user.setDeadLine(0);
        return userMapper.save(user);
    }

    @Transactional
    public User insertUser(User user) {
        //超大型分布式项目如何保证 id 唯一 ，可以借用ip，时间 + 随机数 + 进程id ；  登录时存储在Redis中更需要注意同时登录的人远比同时注册的人多。
        user.setId(new ObjectId(new Date()).toString());

        user.setCreateDate(new Date());
        user.setIsVIP(0);
        user.setUserCoin(100);
        user.setDeadLine(0);
        return userMapper.save(user);
    }
}
